package chapter2.src.com.kkb.array;

import org.junit.jupiter.api.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

class TestTreeMap {


    public static TreeMap<String, String> getDateIncomeMap() {
        TreeMap<String, String> map = new TreeMap<>((var1, var2) ->

        {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date date1 = simpleDateFormat.parse(var1);
                Date date2 = simpleDateFormat.parse(var2);
                return date1.compareTo(date2);
            } catch (ParseException e) {
                e.printStackTrace();
                return -1;
            }
        }
        );
        map.put("2020-01-01", "11");
        map.put("2020-01-04", "44");
        map.put("2020-01-03", "33");
        map.put("2020-01-02", "22");
        map.put("2020-01-06", "66");
        map.put("2020-01-05", "55");

        return map;
    }

    void testIterator() {
        TreeMap<String, Integer> map = new TreeMap<>();
        map.put("A", 1);
        map.put("B", 2);
        map.put("C", 3);
        Iterator<String> iterator = map.keySet().iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            System.out.println(key + ":" + map.get(key));
        }
    }


    void testIterator1() {
        TreeMap<String, Integer> map = new TreeMap<>();
        map.put("A", 1);
        map.put("B", 2);
        map.put("C", 3);
        Iterator<Map.Entry<String, Integer>> iterator1 = map.entrySet().iterator();
        while (iterator1.hasNext()) {
            Map.Entry<String, Integer> entry = iterator1.next();
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
    }


    @Test
    public void testSort() {

     //   TreeMap<String, String> map = getDateIncomeMap();
     //   System.out.println(map.toString());
//

    }


    public static int compareDateStr(String var1, String var2) {


        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date1 = simpleDateFormat.parse(var1);
            Date date2 = simpleDateFormat.parse(var2);
            return date1.compareTo(date2);
        } catch (ParseException e) {
            e.printStackTrace();
            return -1;
        }


    }


    public static void main(String[] args) {
        TreeMap<String, String> map = getDateIncomeMap();

    }


}
